/**
 * Created by Administrator on 2017/1/12.
 */

var roomManager = require('../../../logic/world/roomManager');

module.exports = function(app) {
    return new UserRemote(app);
};

var UserRemote = function(app) {
    this.app = app;
};

var remote = UserRemote.prototype;


remote.auth = function(openid, cb) {
    gPlayerManger.addPlat(openid, function(err, uid) {
        cb(err, uid);
    });
}

/**
 * 登录处理
 * @param msg
 * @param cb (code, openid, info)
 */
remote.login = function(msg, cb) {
    var uid = +msg.uid;
    var sid = msg.sid;
    if (!sid) {
        cb(Code.NETWORK_ERROR); return;
    }

    gSessionManager.userOnline(uid, sid);
    logger.debug("uid=%d login world sid=%s", uid, sid);

    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            cb(Code.DB_ERROR); return;
        }

        var user = player.user;
        if (user.block) {
            cb(Code.PLAYER_BLOCK); return;
        }

        user.info.name = msg.name;
        user.info.headpic = msg.headpic;
        user.info.sex = msg.sex;
        user.info.province = msg.province;

        if (player.isNew) {
            user.info.createIP = msg.ip;
        }

        user.marks.loginTime = Date.getStamp();
        user.marks.loginIP = msg.ip;

        var isSignReward = false;

        if (player.getDayVar("SignReward")) {
            isSignReward = true;
        }

        player.markDirty("info");
        player.markDirty("marks");
        player.checkDayVars();
        player.save(true);

        var content = gGlobalManager.getConfig("content") || {};
        var info = {
            user: user,
            marks: roomManager.getUser(uid),
            notice: content.notice,
            contact: content.contact,
            message: content.message,
            onlineCount: gPlayerManger.getVirtualOnlineCount(),
            config: gGlobalManager.getConfig(),
            signReward: isSignReward
        }

        info.marks.matchId = gMatchManager.getUserMatchId(uid);

        cb(Code.SUCCESS, player.openid, info);
    });
}

remote.offline = function(uid, callback) {
    callback();

    gSessionManager.userOffline(uid);
    logger.debug("uid=%d offline world", uid);

    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            return;
        }

        var user = player.user;

        user.marks.logoutTime = Date.getStamp();
        player.markDirty("marks");

        player.save(true);
    });
}

/**
 * 远程调用扣除资源
 * @param uid
 * @param resource
 * @param callback
 */
remote.costResource = function(uid, resource, reason, callback) {
    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            callback({code:Code.UID_INVALID}); return;
        }

        player.alterResourceNotSync(ExObject.minus(resource), reason);
        callback({status: player.user.status});
    });
}

/**
 * 远程调用添加资源
 * @param uid
 * @param resource
 * @param callback
 */
remote.addResource = function(uid, resource, reason, callback) {
    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            callback({code:Code.UID_INVALID}); return;
        }

        player.alterResourceNotSync(resource, reason);
        callback({status: player.user.status});
    });
}

/**
 * 远程调用修改资源
 * @param uid
 * @param resource
 * @param callback
 */
remote.alterResource = function(uid, resource, reason, callback) {
    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            callback({code:Code.UID_INVALID}); return;
        }

        player.alterResourceNotSync(resource, reason);
        callback({status: player.user.status});
    });
}

/**
 * 远程调用批量修改资源
 * @param resources，{uid: {resource}}
 * @param callback
 */
remote.batchAlterResource = function (resources, reason, callback) {
    async.each(ExObject.numberKeys(resources), function(uid, cb) {
        gPlayerManger.getPlayer(uid, function(player) {
            if (player) {
                player.alterResourceNotSync(resources[uid], reason);
            }
            cb();
        });
    }, function(err) {
        callback();
    });
}

remote.pushMessage = function(uid, route, message, callback) {
    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            callback({code:Code.UID_INVALID}); return;
        }
        player.send(route, message);
        callback({});
    });
}